package com.inossem.warehousestorage_inossem.activity.change_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageManageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ChangeStorageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

public class ChangeStorageMaterielDetailActivity extends BaseActivity {

    private static final String TRUE = "1";

    private static final String FALSE = "0";

    private TextView title, materielCode, materielDescription, batch, date, position, sourceSpecialInventoryCode, sourceSpecialInventoryDescription;

    private TextView targetMaterielCode, targetMaterielDescription, targetFactory, targetInventoryAddress, targetSpecialInventoryCode, targetSpecialInventoryDescription;

    private TextView targetFactoryCode, targetInventoryAddressCode, targetWarehouseNumber;

    private EditText number;

    private String mId, mFactoryCode, mInventoryAddressCode, mSpecialInventory;

    private List<ChangeStorageManageBean.DataBean.FactoryListBean> mTargetFactoryList;

    private ChangeStorageMaterielListBean.DataBean.MaterielListBean materielItem;

    private PercentRelativeLayout targetMaterielLayout, targetFactoryLayout, targetSpecialInventoryLayout, targetMaterielDescriptionLayout, targetInventoryAddressLayout, targetSpecialInventoryDescriptionLayout;

    public static final int SELECT_MATERIEL = 100;

    public static final int SELECT_SPECIAL_INVENTORY = 200;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_storage_materiel_detail);
        initView(ChangeStorageMaterielDetailActivity.this);
        initData(ChangeStorageMaterielDetailActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielCode = (TextView) activity.findViewById(R.id.materielCode);
        materielDescription = (TextView) activity.findViewById(R.id.materielDescription);
        batch = (TextView) activity.findViewById(R.id.batch);
        date = (TextView) activity.findViewById(R.id.date);
        position = (TextView) activity.findViewById(R.id.position);
        sourceSpecialInventoryCode = (TextView) activity.findViewById(R.id.sourceSpecialInventoryCode);
        sourceSpecialInventoryDescription = (TextView) activity.findViewById(R.id.sourceSpecialInventoryDescription);
        targetMaterielCode = (TextView) activity.findViewById(R.id.targetMaterielCode);
        targetMaterielDescription = (TextView) activity.findViewById(R.id.targetMaterielDescription);
        targetFactory = (TextView) activity.findViewById(R.id.targetFactory);
        targetInventoryAddress = (TextView) activity.findViewById(R.id.targetInventoryAddress);
        targetSpecialInventoryCode = (TextView) activity.findViewById(R.id.targetSpecialInventoryCode);
        targetSpecialInventoryDescription = (TextView) activity.findViewById(R.id.targetSpecialInventoryDescription);
        targetFactoryCode = (TextView) activity.findViewById(R.id.targetFactoryCode);
        targetInventoryAddressCode = (TextView) activity.findViewById(R.id.targetInventoryAddressCode);
        targetWarehouseNumber = (TextView) activity.findViewById(R.id.targetWarehouseNumber);

        number = (EditText) activity.findViewById(R.id.number);

        targetMaterielLayout = (PercentRelativeLayout) activity.findViewById(R.id.targetMaterielLayout);
        targetMaterielDescriptionLayout = (PercentRelativeLayout) activity.findViewById(R.id.targetMaterielDescriptionLayout);
        targetFactoryLayout = (PercentRelativeLayout) activity.findViewById(R.id.targetFactoryLayout);
        targetInventoryAddressLayout = (PercentRelativeLayout) activity.findViewById(R.id.targetInventoryAddressLayout);
        targetSpecialInventoryLayout = (PercentRelativeLayout) activity.findViewById(R.id.targetSpecialInventoryLayout);
        targetSpecialInventoryDescriptionLayout = (PercentRelativeLayout) activity.findViewById(R.id.targetSpecialInventoryDescriptionLayout);

    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_materiel_detail_title));
        ActivityUtils.initBackAndFunction(activity, "管理", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                backAndUpdate();
            }
        });
        getExtra(activity);
        ChangeStorageBean bean = ChangeStorageUtils.getChangeStorageBean(activity);
        mFactoryCode = bean.getFactoryCode();
        mInventoryAddressCode = bean.getInventoryAddressCode();
        mSpecialInventory = bean.getSpecialInventory();
        mTargetFactoryList = bean.getTargetFactoryList();
        getData(activity);
    }

    private void getExtra(Activity activity) {
        mId = activity.getIntent().getStringExtra(ChangeStorageExtra.ID);
    }

    private void initTargetMateriel(Context context) {
        if (TRUE.equals(materielItem.getTargetMaterielFlag())) {
            targetMaterielLayout.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(materielItem.getTargetMaterielCode())) {
                targetMaterielCode.setText("请选择物料编码");
                targetMaterielDescriptionLayout.setVisibility(View.GONE);
            } else {
                targetMaterielDescriptionLayout.setVisibility(View.VISIBLE);
                targetMaterielCode.setText(materielItem.getTargetMaterielCode());
                targetMaterielDescription.setText(materielItem.getTargetMaterielDescription());
            }
            targetMaterielCode.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Intent intent = new Intent(context, ChangeStorageSelectMaterielActivity.class);
                    intent.putExtra(ChangeStorageExtra.FACTORY_CODE, mFactoryCode);
                    intent.putExtra(ChangeStorageExtra.INVENTORY_ADDRESS_CODE, mInventoryAddressCode);
                    intent.putExtra(ChangeStorageExtra.SPECIAL_INVENTORY, mSpecialInventory);
                    startActivityForResult(intent, SELECT_MATERIEL);
                }
            });
        } else {
            targetMaterielLayout.setVisibility(View.GONE);
        }
    }

    private void initTargetSpecialInventory(Context context) {
        if (TRUE.equals(materielItem.getTargetSpecialInventoryFlag())) {
            targetSpecialInventoryLayout.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(materielItem.getTargetSpecialInventoryCode())) {
                targetSpecialInventoryCode.setText("请选择特殊库存代码");
                targetSpecialInventoryDescriptionLayout.setVisibility(View.GONE);
            } else {
                targetSpecialInventoryDescriptionLayout.setVisibility(View.VISIBLE);
                targetSpecialInventoryCode.setText(materielItem.getTargetSpecialInventoryCode());
                targetSpecialInventoryDescription.setText(materielItem.getTargetSpecialInventoryDescription());
            }
            targetSpecialInventoryCode.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Intent intent = new Intent(context, ChangeStorageSelectSpecialInventoryActivity.class);
                    intent.putExtra(ChangeStorageExtra.SPECIAL_INVENTORY, mSpecialInventory);
                    startActivityForResult(intent, SELECT_SPECIAL_INVENTORY);
                }
            });
        } else {
            targetSpecialInventoryLayout.setVisibility(View.GONE);
        }
    }

    private void initTargetFactory(Context context) {
        if (TextUtils.isEmpty(materielItem.getTargetFactoryCode())) {
            targetFactory.setText("请选择工厂");
            targetInventoryAddressLayout.setVisibility(View.GONE);
        } else {
            targetInventoryAddressLayout.setVisibility(View.VISIBLE);
            targetFactory.setText(materielItem.getTargetFactoryValue());
            targetFactoryCode.setText(materielItem.getTargetFactoryCode());
            if (TextUtils.isEmpty(materielItem.getTargetInventoryAddressCode())) {
                targetInventoryAddress.setText("请选择库存地点");
            } else {
                targetInventoryAddress.setText(materielItem.getTargetInventoryAddressValue());
                targetInventoryAddressCode.setText(materielItem.getTargetInventoryAddressCode());
            }
            targetInventoryAddress.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    DialogUtils.getChangeStorageInventoryAddressDialog(context, ChangeStorageUtils.getInventoryAddressListByFactoryCode(context, materielItem.getTargetFactoryCode()), new DialogUtils.ChangeStorageInventoryAddressListener() {
                        @Override
                        public void onSelect(Dialog dialog, String text, String value, String warehouseNumber) {
                            if (!value.equals(targetInventoryAddressCode.getText().toString())) {
                                targetInventoryAddress.setText(value + " - " + text);
                                targetInventoryAddressCode.setText(value);
                                targetWarehouseNumber.setText(warehouseNumber);
                            }
                        }
                    }).show();
                }
            });
        }
        targetFactory.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getChangeStorageFactoryDialogNew(context, mTargetFactoryList, true, new DialogUtils.ChangeStorageFactoryListener() {
                    @Override
                    public void onSelect(Dialog dialog, String text, String value, List<ChangeStorageManageBean.DataBean.InventoryAddressListBean> inventoryAddressList) {
                        if (!value.equals(targetFactoryCode.getText().toString())) {
                            targetFactory.setText(value + " - " + text);
                            targetFactoryCode.setText(value);
                            resetInventorAddress(context);
                            targetInventoryAddressLayout.setVisibility(View.VISIBLE);
                            targetInventoryAddress.setOnClickListener(new NoDoubleClickListener() {
                                @Override
                                public void onNoDoubleClick(View v) {
                                    DialogUtils.getChangeStorageInventoryAddressDialog(context, inventoryAddressList, new DialogUtils.ChangeStorageInventoryAddressListener() {
                                        @Override
                                        public void onSelect(Dialog dialog, String text, String value, String warehouseNumber) {
                                            if (!value.equals(targetInventoryAddressCode.getText().toString())) {
                                                targetInventoryAddress.setText(value + " - " + text);
                                                targetInventoryAddressCode.setText(value);
                                                targetWarehouseNumber.setText(warehouseNumber);
                                            }
                                        }
                                    }).show();
                                }
                            });
                        }
                    }
                }).show();
            }
        });
    }

    private void getData(final Context context) {
        materielItem = ChangeStorageUtils.getMaterielById(context, mId);
        materielCode.setText(materielItem.getMaterielCode());
        materielDescription.setText(materielItem.getMaterielDescription());
        batch.setText(materielItem.getBatch());
        date.setText(materielItem.getDate());
        position.setText(materielItem.getPosition());
        sourceSpecialInventoryCode.setText(materielItem.getSpecialInventoryCode());
        sourceSpecialInventoryDescription.setText(materielItem.getSpecialInventoryDescription());

        number.setText(TextUtils.isEmpty(materielItem.getNumber()) ? "" : (materielItem.getNumber() + materielItem.getUnit()));
        number.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            if (hasFocus) {
                number.setText("");
            }
        });

        initTargetMateriel(context);
        initTargetFactory(context);
        initTargetSpecialInventory(context);

    }

    private void resetInventorAddress(Context context) {
        targetInventoryAddress.setText(context.getResources().getString(R.string.activity_in_storage_other_index_inventory_address_hint));
        targetInventoryAddressCode.setText("");
        targetInventoryAddressLayout.setVisibility(View.GONE);
    }

    private void updateInfo(Context context) {
        String idStr = materielItem.getId();
        String numberStr = number.getText().toString().replace(materielItem.getUnit(), "").trim();
        String targetMaterielCodeStr = TRUE.equals(materielItem.getTargetMaterielFlag()) ? targetMaterielCode.getText().toString() : null;
        String targetMaterielDescriptionStr = TRUE.equals(materielItem.getTargetMaterielFlag()) ? targetMaterielDescription.getText().toString() : null;
        String targetFactoryCodeStr = targetFactoryCode.getText().toString();
        String targetFactoryValueStr = targetFactory.getText().toString();
        String targetInventoryAddressCodeStr = targetInventoryAddressCode.getText().toString();
        String targetInventoryAddressValueStr = targetInventoryAddress.getText().toString();
        String targetWarehouseNumberStr = targetWarehouseNumber.getText().toString();
        String targetSpecialInventoryCodeStr = TRUE.equals(materielItem.getTargetSpecialInventoryFlag()) ? targetSpecialInventoryCode.getText().toString() : null;
        String targetSpecialInventoryDescriptionStr = TRUE.equals(materielItem.getTargetSpecialInventoryFlag()) ? targetSpecialInventoryDescription.getText().toString() : null;
//        LogUtils.i("targetMaterielCodeStr:" + targetMaterielCodeStr);
//        LogUtils.i("targetMaterielDescriptionStr:" + targetMaterielDescriptionStr);
//        LogUtils.i("targetFactoryCodeStr:" + targetFactoryCodeStr);
//        LogUtils.i("targetFactoryValueStr:" + targetFactoryValueStr);
//        LogUtils.i("targetInventoryAddressCodeStr:" + targetInventoryAddressCodeStr);
//        LogUtils.i("targetInventoryAddressValueStr:" + targetInventoryAddressValueStr);
//        LogUtils.i("targetWarehouseNumberStr:" + targetWarehouseNumberStr);
//        LogUtils.i("targetSpecialInventoryCodeStr:" + targetSpecialInventoryCodeStr);
//        LogUtils.i("targetSpecialInventoryDescriptionStr:" + targetSpecialInventoryDescriptionStr);
        ChangeStorageUtils.updateSpecialInventoryById(context, idStr, numberStr, targetMaterielCodeStr, targetMaterielDescriptionStr, targetFactoryCodeStr, targetFactoryValueStr, targetInventoryAddressCodeStr, targetInventoryAddressValueStr, targetWarehouseNumberStr, targetSpecialInventoryCodeStr, targetSpecialInventoryDescriptionStr);
    }

    private String check(Context context) {
        String numberStr = number.getText().toString().replace(materielItem.getUnit(), "").trim();
        if (TextUtils.isEmpty(numberStr)) {
            return "调整数量不可以为空";
        }

        if (!RegexUtils.checkDecimal(numberStr)) {
            return "调整数量不合法";
        }

        //物料检测
        if (TRUE.equals(materielItem.getTargetMaterielFlag())) {
            if (TextUtils.isEmpty(targetMaterielDescription.getText().toString())) {
                return "请选择物料编码";
            }
        }

        //检测工厂和库存地点
        if (TextUtils.isEmpty(targetFactoryCode.getText().toString())) {
            return "请选择工厂";
        }
        if (TextUtils.isEmpty(targetInventoryAddressCode.getText().toString())) {
            return "请选择库存地点";
        }

        //检测特殊库存
        if (TRUE.equals(materielItem.getTargetSpecialInventoryFlag())) {
            if (TextUtils.isEmpty(targetSpecialInventoryDescription.getText().toString())) {
                return "请选择特殊库存代码";
            }
        }
        return null;
    }

    private void backAndUpdate() {
        String checkResult = check(ChangeStorageMaterielDetailActivity.this);
        if (checkResult == null) {
            updateInfo(ChangeStorageMaterielDetailActivity.this);
            super.onBackPressed();
        } else {
            DialogUtils.getConfirmDialog(ChangeStorageMaterielDetailActivity.this, checkResult + "\n" + "是否强制退出?", new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    ChangeStorageMaterielDetailActivity.super.onBackPressed();
                }
            }).show();
        }
    }

    @Override
    public void onBackPressed() {
        backAndUpdate();
    }

    @Override
    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            switch (requestCode) {
                case SELECT_MATERIEL:
                    targetMaterielCode.setText(data.getStringExtra(ChangeStorageExtra.MATERIEL_CODE));
                    targetMaterielDescription.setText(data.getStringExtra(ChangeStorageExtra.MATERIEL_DESCRIPTION));
                    targetMaterielDescriptionLayout.setVisibility(View.VISIBLE);
                    break;
                case SELECT_SPECIAL_INVENTORY:
                    targetSpecialInventoryCode.setText(data.getStringExtra(ChangeStorageExtra.SPECIAL_INVENTORY_CODE));
                    targetSpecialInventoryDescription.setText(data.getStringExtra(ChangeStorageExtra.SPECIAL_INVENTORY_DESCRIPTION));
                    targetSpecialInventoryDescriptionLayout.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }
}
